package com.cckp.utils;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 * @author lijiasheng2  2021/11/2.
 */
public class Pic {

    public static void main(String[] args)throws IOException {
        String p = "D:\\job\\cb\\20211102灯片导入图片颜色变化\\图片_20211102155054\\";
        System.out.println(isRgbOrCmyk(p + "3014~Mask~涂抹面膜眼膜#2~20211020113526~996×230.jpg"));
        System.out.println(isRgbOrCmyk(p + "3014~Mask~涂抹面膜眼膜#2~20211020113715~945×171.jpg"));
    }

    public static boolean isRgbOrCmyk(String filename) throws IOException {
        File file = new File(filename);
        boolean isRgb = true;
        //创建输入流
        ImageInputStream input = ImageIO.createImageInputStream(file);
        Iterator readers = ImageIO.getImageReaders(input);
        if (readers == null || !readers.hasNext()) {
            throw new RuntimeException("No ImageReaders found");
        }
        ImageReader reader = (ImageReader) readers.next();
        reader.setInput(input);
        //获取文件格式
        BufferedImage image;
        try {
            // 尝试读取图片 (包括颜色的转换).
            reader.read(0); // RGB
        } catch (IIOException e) {
            // 读取Raster (没有颜色的转换).
            reader.readRaster(0, null);// CMYK
            isRgb = false;
        }
        return isRgb;
    }
}
